package com.hontech.bvapp2.ui.parts.cargo

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Cargo
import com.hontech.bvapp2.ui.dialog.runProg
import com.hontech.bvapp2.ui.dialog.runProgErr
import com.hontech.bvapp2.ui.view.LabelSpinner
import com.hontech.bvapp2.ui.view.LabelSwitch
import com.hontech.bvapp2.utils.ExecException
import com.hontech.bvapp2.utils.onClick

class CargoComParts(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_cargo_com, this)
        val attr = context.obtainStyledAttributes(attrs, R.styleable.CargoComParts)
        val cargo = attr.getInteger(R.styleable.CargoComParts_com_cargo, 0)
        attr.recycle()

        val scan = view.findViewById<Button>(R.id.id_cargo_com_btn_scan)
        val swLed = view.findViewById<LabelSwitch>(R.id.id_cargo_com_sw_led)
        val spRows = view.findViewById<LabelSpinner>(R.id.id_cargo_com_sp_rows)
        val spCols = view.findViewById<LabelSpinner>(R.id.id_cargo_com_sp_cols)
        val ctrl = view.findViewById<Button>(R.id.id_cargo_com_btn_ctrl)
        val scanRow = view.findViewById<Button>(R.id.id_cargo_com_btn_scan_row)
        val scanAll = view.findViewById<Button>(R.id.id_cargo_com_btn_scan_all)

        scan.onClick {
            runProg("扫码", "") {
                Cargo.scanLed(cargo, 0)
                val barcode = Cargo.scan(cargo)
                it.setMsg(barcode)
                Cargo.scanLed(cargo, 1)
            }
        }

        swLed.onToggleFn = {
            runProgErr("补关灯") {
                val c = if (it) 0 else 1
                Cargo.scanLed(cargo, c)
            }
        }

        ctrl.onClick {
            val col = spCols.selectIndex() + 1
            val row = spRows.selectIndex() + 1
            runProg("货架", "移动: $row-$col") {
                val cp = Cargo.cargoPositionWithCol(col)
                val rp = Cargo.cargoPickPositionWithRow(row)
                Cargo.rowColCtrl(cargo, rp, cp)
            }
        }

        scanRow.onClick {
            val row = spRows.selectIndex() + 1
            runProg("扫码整行", "") {
                Cargo.scanLed(cargo, 0)
                val rp = Cargo.cargoScanPositionWithRow(row)
                Cargo.cargoCtrl(cargo, rp)
                for (col in 1..6) {
                    val cp = Cargo.cargoPositionWithCol(col)
                    Cargo.pickCtrl(cargo, cp)

                    try {
                        val barcode = Cargo.scan(cargo)
                        it.setMsg("$row-$col => $barcode")
                    } catch (e: ExecException) {
                        e.printStackTrace()
                        val msg = e.message ?: "扫码错误"
                        it.setMsg("$row-$col => $msg")
                    }
                }
                Cargo.scanLed(cargo, 1)
            }
        }

        scanAll.onClick {
            runProg("扫码全部", "") {
                Cargo.scanLed(cargo, 0)
                var row = 1
                while (row <= 25) {
                    val rp = Cargo.cargoScanPositionWithRow(row)
                    Cargo.cargoCtrl(cargo, rp)
                    var col = 1
                    while (col <= 6) {
                        val cp = Cargo.cargoPositionWithCol(col)
                        Cargo.pickCtrl(cargo, cp)

                        try {
                            val barcode = Cargo.scan(cargo)
                            it.setMsg("$row-$col => $barcode")
                        } catch (e: ExecException) {
                            e.printStackTrace()
                            val msg = e.message ?: "扫码错误"
                            it.setMsg("$row-$col => $msg")
                        }
                        col += 1
                    }
                    row += 1
                    if (row > 25) {
                        break
                    }

                    val rp1 = Cargo.cargoScanPositionWithRow(row)
                    Cargo.cargoCtrl(cargo, rp1)
                    col = 6
                    while (col >= 1) {
                        val cp = Cargo.cargoPositionWithCol(col)
                        Cargo.pickCtrl(cargo, cp)

                        try {
                            val barcode = Cargo.scan(cargo)
                            it.setMsg("$row-$col => $barcode")
                        } catch (e: ExecException) {
                            e.printStackTrace()
                            val msg = e.message ?: "扫码错误"
                            it.setMsg("$row-$col => $msg")
                        }
                        col -= 1
                    }
                    row += 1
                }
                Cargo.scanLed(cargo, 1)
            }
        }
    }
}